Nov. branch merge. Various features backported from stable, various bug fixes.
[lhc/web/wiklou.git] / includes / SearchEngine.php
1 <?
2 # See search.doc
3
4 class SearchEngine {
5 /* private */ var $mUsertext, $mSearchterms;
6 /* private */ var $mTitlecond, $mTextcond;
7
8 var $doSearchRedirects = true;
9 var $addtoquery = array();
10 var $namespacesToSearch = array();
11 var $alternateTitle;
12
13 function SearchEngine( $text )
14 {
15 # We display the query, so let's strip it for safety
16 #
17 global $wgDBmysql4;
18 $lc = SearchEngine::legalSearchChars() . "()";
19 if( $wgDBmysql4 ) $lc .= "\"~<>*+-";
20 $this->mUsertext = trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
21 $this->mSearchterms = array();
22 }
23
24 function queryNamespaces()
25 {
26 $namespaces = implode( ",", $this->namespacesToSearch );
27 if ($namespaces == "") {
28 $namespaces = "0";
29 }
30 return "AND cur_namespace IN (" . $namespaces . ")";
31 #return "1";
32 }
33
34 function searchRedirects()
35 {
36 if ( $this->doSearchRedirects ) return "";
37 return "AND cur_is_redirect=0 ";
38 }
39
40 /* private */ function initNamespaceCheckbox( $i )
41 {
42 global $wgUser, $wgNamespacesToBeSearchedDefault;
43
44
45 if ($wgUser->getID()) {
46 // User is logged in so we retrieve his default namespaces
47 return $wgUser->getOption( "searchNs".$i );
48 }
49 else {
50 // User is not logged in so we give him the global default namespaces
51 return $wgNamespacesToBeSearchedDefault[ $i ];
52 }
53 }
54
55 # Display the "power search" footer. Does not actually perform the search,
56 # that is done by showResults()
57 function powersearch()
58 {
59 global $wgUser, $wgOut, $wgLang, $wgTitle;
60
61 $search = $_REQUEST['search'];
62 $searchx = $_REQUEST['searchx'];
63 $listredirs = $_REQUEST['redirs'];
64
65 $ret = wfMsg("powersearchtext"); # Text to be returned
66 $tempText = ""; # Temporary text, for substitution into $ret
67
68 # Do namespace checkboxes
69 $namespaces = $wgLang->getNamespaces();
70 foreach ( $namespaces as $i => $namespace ) {
71 # Skip virtual namespaces
72 if ( $i < 0 ) {
73 continue;
74 }
75
76 $formVar = "ns$i";
77
78 # Initialise checkboxValues, either from defaults or from
79 # a previous invocation
80 if ( !isset( $searchx ) ) {
81 $checkboxValue = $this->initNamespaceCheckbox( $i );
82 } else {
83 $checkboxValue = $_REQUEST[$formVar];
84 }
85
86 $checked = "";
87 if ( $checkboxValue == 1 ) {
88 $checked = " checked";
89 $this->addtoquery["ns{$i}"] = 1;
90 array_push( $this->namespacesToSearch, $i );
91 }
92 $name = str_replace( "_", " ", $namespaces[$i] );
93 if ( "" == $name ) {
94 $name = wfMsg( "blanknamespace" );
95 }
96
97 if ( $tempText !== "" ) {
98 $tempText .= " ";
99 }
100 $tempText .= "<input type=checkbox value=\"1\" name=\"" .
101 "ns{$i}\"{$checked}>{$name}\n";
102 }
103 $ret = str_replace ( "$1", $tempText, $ret );
104
105 # List redirects checkbox
106
107 $checked = "";
108 if ( $listredirs == 1 ) {
109 $this->addtoquery["redirs"] = 1;
110 $checked = " checked";
111 }
112 $tempText = "<input type=checkbox value=1 name=\"redirs\"{$checked}>\n";
113 $ret = str_replace( "$2", $tempText, $ret );
114
115 # Search field
116
117 $tempText = "<input type=text name=\"search\" value=\"" .
118 htmlspecialchars( $search ) ."\" width=80>\n";
119 $ret = str_replace( "$3", $tempText, $ret );
120
121 # Searchx button
122
123 $tempText = "<input type=submit name=\"searchx\" value=\"" .
124 wfMsg("powersearch") . "\">\n";
125 $ret = str_replace( "$9", $tempText, $ret );
126
127 $ret = "<br><br>\n<form id=\"powersearch\" method=\"get\" " .
128 "action=\"" . wfLocalUrl( "" ) . "\">\n{$ret}\n</form>\n";
129
130 if ( isset ( $searchx ) ) {
131 if ( ! $listredirs ) {
132 $this->doSearchRedirects = false;
133 }
134 }
135 return $ret;
136 }
137
138 # Perform the search and construct the results page
139 function showResults()
140 {
141 global $wgUser, $wgTitle, $wgOut, $wgLang, $wgDisableTextSearch;
142 global $wgInputEncoding;
143 $fname = "SearchEngine::showResults";
144
145 $search = $_REQUEST['search'];
146
147 $powersearch = $this->powersearch(); /* Need side-effects here? */
148
149 $wgOut->setPageTitle( wfMsg( "searchresults" ) );
150 $q = wfMsg( "searchquery", htmlspecialchars( $this->mUsertext ) );
151 $wgOut->setSubtitle( $q );
152 $wgOut->setArticleFlag( false );
153 $wgOut->setRobotpolicy( "noindex,nofollow" );
154
155 $sk = $wgUser->getSkin();
156 $text = wfMsg( "searchresulttext", $sk->makeKnownLink(
157 wfMsg( "searchhelppage" ), wfMsg( "searchingwikipedia" ) ) );
158 $wgOut->addHTML( $text );
159
160 $this->parseQuery();
161 if ( "" == $this->mTitlecond || "" == $this->mTextcond ) {
162 $wgOut->addHTML( "<h2>" . wfMsg( "badquery" ) . "</h2>\n" .
163 "<p>" . wfMsg( "badquerytext" ) );
164 return;
165 }
166 list( $limit, $offset ) = wfCheckLimits( 20, "searchlimit" );
167
168 $searchnamespaces = $this->queryNamespaces();
169 $redircond = $this->searchRedirects();
170
171 $sql = "SELECT cur_id,cur_namespace,cur_title," .
172 "cur_text FROM cur,searchindex " .
173 "WHERE cur_id=si_page AND {$this->mTitlecond} " .
174 "{$searchnamespaces} {$redircond}" .
175 "LIMIT {$offset}, {$limit}";
176 $res1 = wfQuery( $sql, DB_READ, $fname );
177 $num = wfNumRows($res1);
178
179 if ( $wgDisableTextSearch ) {
180 $wgOut->addHTML( wfMsg( "searchdisabled", $search, $wgInputEncoding ) );
181 } else {
182 $sk = $wgUser->getSkin();
183 $text = wfMsg( "searchresulttext", $sk->makeKnownLink(
184 wfMsg( "searchhelppage" ), wfMsg( "searchingwikipedia" ) ) );
185 $wgOut->addHTML( $text );
186
187 $this->parseQuery();
188 if ( "" == $this->mTitlecond || "" == $this->mTextcond ) {
189 $wgOut->addHTML( "<h2>" . wfMsg( "badquery" ) . "</h2>\n" .
190 "<p>" . wfMsg( "badquerytext" ) );
191 return;
192 }
193 list( $limit, $offset ) = wfCheckLimits( 20, "searchlimit" );
194
195 $searchnamespaces = $this->queryNamespaces();
196 $redircond = $this->searchRedirects();
197
198 $sql = "SELECT cur_id,cur_namespace,cur_title," .
199 "cur_text FROM cur,searchindex " .
200 "WHERE cur_id=si_page AND {$this->mTitlecond} " .
201 "{$searchnamespaces} {$redircond}" .
202 "LIMIT {$offset}, {$limit}";
203 $res1 = wfQuery( $sql, DB_READ, $fname );
204 $num = wfNumRows($res1);
205
206 $sql = "SELECT cur_id,cur_namespace,cur_title," .
207 "cur_text FROM cur,searchindex " .
208 "WHERE cur_id=si_page AND {$this->mTextcond} " .
209 "{$searchnamespaces} {$redircond} " .
210 "LIMIT {$offset}, {$limit}";
211 $res2 = wfQuery( $sql, DB_READ, $fname );
212 $num = $num + wfNumRows($res2);
213
214 if ( $num == $limit ) {
215 $top = wfShowingResults( $offset, $limit);
216 } else {
217 $top = wfShowingResultsNum( $offset, $limit, $num );
218 }
219 $wgOut->addHTML( "<p>{$top}\n" );
220
221 # For powersearch
222
223 $a2l = "" ;
224 $akk = array_keys( $this->addtoquery ) ;
225 foreach ( $akk AS $ak ) {
226 $a2l .= "&{$ak}={$this->addtoquery[$ak]}" ;
227 }
228
229 $sl = wfViewPrevNext( $offset, $limit, "",
230 "search=" . wfUrlencode( $this->mUsertext ) . $a2l );
231 $wgOut->addHTML( "<br>{$sl}\n" );
232
233 $foundsome = false;
234
235 if ( 0 == wfNumRows( $res1 ) ) {
236 $wgOut->addHTML( "<h2>" . wfMsg( "notitlematches" ) .
237 "</h2>\n" );
238 } else {
239 $foundsome = true;
240 $off = $offset + 1;
241 $wgOut->addHTML( "<h2>" . wfMsg( "titlematches" ) .
242 "</h2>\n<ol start='{$off}'>" );
243
244 while ( $row = wfFetchObject( $res1 ) ) {
245 $this->showHit( $row );
246 }
247 wfFreeResult( $res1 );
248 $wgOut->addHTML( "</ol>\n" );
249 }
250 }
251
252 if ( $wgDisableTextSearch ) {
253 $wgOut->addHTML( wfMsg( "searchdisabled", $search, $wgInputEncoding ) );
254 } else {
255 if ( 0 == wfNumRows( $res2 ) ) {
256 $wgOut->addHTML( "<h2>" . wfMsg( "notextmatches" ) .
257 "</h2>\n" );
258 } else {
259 $foundsome = true;
260 $off = $offset + 1;
261 $wgOut->addHTML( "<h2>" . wfMsg( "textmatches" ) . "</h2>\n" .
262 "<ol start='{$off}'>" );
263 while ( $row = wfFetchObject( $res2 ) ) {
264 $this->showHit( $row );
265 }
266 wfFreeResult( $res2 );
267 $wgOut->addHTML( "</ol>\n" );
268 }
269 if ( ! $foundsome ) {
270 $wgOut->addHTML( "<p>" . wfMsg( "nonefound" ) . "\n" );
271 }
272 $wgOut->addHTML( "<p>{$sl}\n" );
273 $wgOut->addHTML( $powersearch );
274 }
275 }
276
277 function legalSearchChars()
278 {
279 $lc = "A-Za-z_'0-9\\x80-\\xFF\\-";
280 return $lc;
281 }
282
283 function parseQuery()
284 {
285 global $wgDBminWordLen, $wgLang, $wgDBmysql4;
286
287 if( $wgDBmysql4 ) {
288 # Use cleaner boolean search if available
289 return $this->parseQuery4();
290 }
291
292 $lc = SearchEngine::legalSearchChars() . "()";
293 $q = preg_replace( "/([()])/", " \\1 ", $this->mUsertext );
294 $q = preg_replace( "/\\s+/", " ", $q );
295 $w = explode( " ", strtolower( trim( $q ) ) );
296
297 $last = $cond = "";
298 foreach ( $w as $word ) {
299 $word = $wgLang->stripForSearch( $word );
300 if ( "and" == $word || "or" == $word || "not" == $word
301 || "(" == $word || ")" == $word ) {
302 $cond .= " " . strtoupper( $word );
303 $last = "";
304 } else if ( strlen( $word ) < $wgDBminWordLen ) {
305 continue;
306 } else if ( FulltextStoplist::inList( $word ) ) {
307 continue;
308 } else {
309 if ( "" != $last ) { $cond .= " AND"; }
310 $cond .= " (MATCH (##field##) AGAINST ('" .
311 wfStrencode( $word ). "'))";
312 $last = $word;
313 array_push( $this->mSearchterms, "\\b" . $word . "\\b" );
314 }
315 }
316 if ( 0 == count( $this->mSearchterms ) ) { return; }
317
318 $this->mTitlecond = "(" . str_replace( "##field##",
319 "si_title", $cond ) . " )";
320
321 $this->mTextcond = "(" . str_replace( "##field##",
322 "si_text", $cond ) . " AND (cur_is_redirect=0) )";
323 }
324
325 function parseQuery4()
326 {
327 # FIXME: not ready yet! Do not use.
328
329 global $wgLang;
330 $lc = SearchEngine::legalSearchChars();
331 #$q = preg_replace( "/([+-]?)([$lc]+)/e",
332 # "\"$1\" . \$wgLang->stripForSearch(\"$2\")",
333 # $this->mUsertext );
334
335 $q = $this->mUsertext;
336 $qq = wfStrencode( $wgLang->stripForSearch( $q ) );
337 $this->mSearchterms = preg_split( '/\s+/', $q );
338 $this->mTitlecond = " MATCH(si_title) AGAINST('$qq' IN BOOLEAN MODE)";
339 $this->mTextcond = " (MATCH(si_text) AGAINST('$qq' IN BOOLEAN MODE) AND cur_is_redirect=0)";
340 }
341
342 function showHit( $row )
343 {
344 global $wgUser, $wgOut;
345
346 $t = Title::makeName( $row->cur_namespace, $row->cur_title );
347 $sk = $wgUser->getSkin();
348
349 $contextlines = $wgUser->getOption( "contextlines" );
350 if ( "" == $contextlines ) { $contextlines = 5; }
351 $contextchars = $wgUser->getOption( "contextchars" );
352 if ( "" == $contextchars ) { $contextchars = 50; }
353
354 $link = $sk->makeKnownLink( $t, "" );
355 $size = str_replace( "$1", strlen( $row->cur_text ), WfMsg( "nbytes" ) );
356 $wgOut->addHTML( "<li>{$link} ({$size})" );
357
358 $lines = explode( "\n", $row->cur_text );
359 $pat1 = "/(.*)(" . implode( "|", $this->mSearchterms ) . ")(.*)/i";
360 $lineno = 0;
361
362 foreach ( $lines as $line ) {
363 if ( 0 == $contextlines ) { break; }
364 --$contextlines;
365 ++$lineno;
366 if ( ! preg_match( $pat1, $line, $m ) ) { continue; }
367
368 $pre = $m[1];
369 if ( 0 == $contextchars ) { $pre = "..."; }
370 else {
371 if ( strlen( $pre ) > $contextchars ) {
372 $pre = "..." . substr( $pre, -$contextchars );
373 }
374 }
375 $pre = wfEscapeHTML( $pre );
376
377 if ( count( $m ) < 3 ) { $post = ""; }
378 else { $post = $m[3]; }
379
380 if ( 0 == $contextchars ) { $post = "..."; }
381 else {
382 if ( strlen( $post ) > $contextchars ) {
383 $post = substr( $post, 0, $contextchars ) . "...";
384 }
385 }
386 $post = wfEscapeHTML( $post );
387 $found = wfEscapeHTML( $m[2] );
388
389 $line = "{$pre}{$found}{$post}";
390 $pat2 = "/(" . implode( "|", $this->mSearchterms ) . ")/i";
391 $line = preg_replace( $pat2,
392 "<font color='red'>\\1</font>", $line );
393
394 $wgOut->addHTML( "<br><small>{$lineno}: {$line}</small>\n" );
395 }
396 $wgOut->addHTML( "</li>\n" );
397 }
398
399 function goResult()
400 {
401 global $wgOut;
402 $fname = "SearchEngine::goResult";
403
404 $search = $_REQUEST['search'];
405
406 # First try to go to page as entered
407 #
408 $t = Title::newFromText( $search );
409
410 if ( 0 != $t->getArticleID() ) {
411 $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
412 return;
413 }
414
415 # Now try all lower case (i.e. first letter capitalized)
416 #
417 $t = Title::newFromText( strtolower( $search ) );
418 if ( 0 != $t->getArticleID() ) {
419 $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
420 return;
421 }
422
423 # Now try capitalized string
424 #
425 $t = Title::newFromText( ucwords( strtolower( $search ) ) );
426 if ( 0 != $t->getArticleID() ) {
427 $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
428 return;
429 }
430
431 # Now try all upper case
432 #
433 $t = Title::newFromText( strtoupper( $search ) );
434 if ( 0 != $t->getArticleID() ) {
435 $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
436 return;
437 }
438
439 # Try a near match
440 #
441 if( !$wgDisableTextSearch ) {
442 $this->parseQuery();
443 $sql = "SELECT cur_id,cur_title,cur_namespace,si_page FROM cur,searchindex " .
444 "WHERE cur_id=si_page AND {$this->mTitlecond} ORDER BY cur_namespace LIMIT 1";
445
446 if ( "" != $this->mTitlecond ) {
447 $res = wfQuery( $sql, DB_READ, $fname );
448 }
449 if ( isset( $res ) && 0 != wfNumRows( $res ) ) {
450 $s = wfFetchObject( $res );
451
452 $t = Title::makeTitle( $s->cur_namespace, $s->cur_title );
453 $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
454 return;
455 }
456 }
457 $wgOut->addHTML( wfMsg("nogomatch",
458 htmlspecialchars( wfLocalUrl( ucfirst($this->mUsertext), "action=edit") ) )
459 . "\n<p>" );
460 $this->showResults();
461 }
462 }
463
464 ?>